package com.vitabuybuy.tv.ui.mine.address.viewmodel

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.net.loadingtype.LoadingType
import com.library.common.toast.XToast
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.AddressListDto
import com.vitabuybuy.tv.inter.OnAddressItemClickListener
import com.vitabuybuy.tv.network.AddressRepository
import com.vitabuybuy.tv.network.AuthRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class AddressViewModel : BaseViewModel() {

    private val itemOnClickListener = object : OnAddressItemClickListener {
        override fun onItemClick(item: AddressListDto, position: Int) {
            defUI.msgEvent.postValue(MessageModel(6, obj = item))
        }

        override fun onEditClick(item: AddressListDto, position: Int) {
            defUI.msgEvent.postValue(MessageModel(4, obj = item))
        }
    }

    val addressList = MutableLiveData<MutableList<AddressListDto>>()

    //网格布局的adapter绑定
    val addressBinding: OnItemBind<AddressListDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: AddressListDto ->
            itemBinding.set(BR.itemBean, R.layout.item_address).bindExtra(BR.position, position)
                .bindExtra(BR.listenner, itemOnClickListener)
        }

    fun getDataList() {
        rxHttpRequest {
            onRequest = {
                addressList.value = AddressRepository.getAddressList().await()
                statusView.set(LoadingType.SHOW_CONTENT)
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = true
        }
    }

    fun getRegionList(regionId: Int?) {
        rxHttpRequest {
            onRequest = {
                val it = AddressRepository.getRegionList(regionId).await()
                defUI.msgEvent.postValue(MessageModel(if (regionId == null) 3 else 1, obj = it))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun saveOrupdateAddress(
        receiverName: String,
        receiverPhone: String,
        regionId: Int,
        detailArea: String,
        zipCode: String,
        isDefault: Boolean?,
        id: Int
    ) {

        rxHttpRequest {
            onRequest = {
                AddressRepository.saveOrupdateAddress(
                    receiverName,
                    receiverPhone,
                    regionId,
                    detailArea,
                    zipCode,
                    isDefault,
                    id
                ).await()
                defUI.msgEvent.postValue(MessageModel(2, arg1 = id))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun deleteAddress(id: Int) {
        rxHttpRequest {
            onRequest = {
                AddressRepository.deleteAddress(id).await()
                defUI.msgEvent.postValue(MessageModel(5))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
